// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// o [author] rhyme_lph
// y [email]  rhymelph@gmail.com
// w [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/journal_account_amount.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/widgets/bottom_sheet/list_select_bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/page/all_reports/journal/account_amount_page.dart';

class AccountAmountProvider extends BaseProvider {
  TextEditingController amountController = TextEditingController();
  FocusNode amountFocusNode = FocusNode();

  CoaCoaAccountHeaderCoaAccountDetail account;

  SettingDataTax gstRate = Config.settingInfo.taxData;

  bool isCheckInclusive = true;

  bool get enable => amountController.text.length != 0 && gstRate != null;

  bool isShowDelete = false;

  @override
  Future get dataFuture => null;

  @override
  void dispose() {
    super.dispose();
    amountController.dispose();
    amountFocusNode.dispose();
  }

  @override
  void initState() {
    super.initState();
    amountController.addListener(_handleInputUpdate);
  }

  void onDone() {
    obtainContext?.call((context) async {
      if (isShowDelete == true) {
        Navigator.of(context)
          ..pop(JournalAccountAmount()
            ..amount = double.parse(amountController.text)
            ..gst = gstRate
            ..isCheckInclusive = isCheckInclusive
            ..account = account);
      } else {
        Navigator.of(context)
          ..pop()
          ..pop(JournalAccountAmount()
            ..amount = double.parse(amountController.text)
            ..gst = gstRate
            ..isCheckInclusive = isCheckInclusive
            ..account = account);
      }
    });
  }

  void _handleInputUpdate() {
    update();
  }

  void onChangeAccount() async {
    update();
    if (isShowDelete == true) {
      final result = await Routes.navigateTo(
          RouterGenProviders.accountsSelectPage,
          params: {
            'isSelect': true,
          });
      print('result:$result');
      if (result != null) {
        account = result;
        update();
      }
    } else {
      obtainContext?.call((context) {
        Navigator.of(context).pop();
      });
    }
  }

  void onSelectGstRate() {
    amountFocusNode.unfocus();
    obtainContext?.call((context) async {
      gstRate = await showGstRateBottomSheet(context, gstRate);
      update();
    });
  }

  void onChangeInclusive() {
    isCheckInclusive = !isCheckInclusive;
    update();
  }

  String getGSTInfo() {
    double amount = double.parse(amountController.text, (e) => 0.0);
    double rateDouble = gstRate.rate / 100;
    if (isCheckInclusive == true) {
      double sourceAmount = amount / (1 + rateDouble);
      double inclusive = amount - sourceAmount;
      return CashUtils.formatCashAmount(inclusive);
    } else {
      double gst = amount * rateDouble;
      return CashUtils.formatCashAmount(gst);
    }
  }

  String getTotal() {
    double amount = double.parse(amountController.text, (e) => 0.0);
    double rateDouble = gstRate.rate / 100;
    if (isCheckInclusive == true) {
      return CashUtils.formatCashAmount(amount);
    } else {
      double result = amount + amount * rateDouble;
      return CashUtils.formatCashAmount(result);
    }
  }

  void onDelete() {
    obtainContext?.call((context) {
      Navigator.of(context).pop(true);
    });
  }
}
